<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Comment.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/Textarea.php");
require_once("HTML/QuickForm/Hidden.php");
require_once("HTML/QuickForm/Submit.php");

/**
 * Action closure page.<br/>
 * Form to be used by a user to change the status of an action to CLOSED.
 */
class CloseActionPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
		
		$l_itemInput=new HTML_QuickForm_hidden("itemId");
		$this->addElement($l_itemInput);
		
		$l_noteInput=new HTML_QuickForm_textarea("note");
		$l_noteInput->setLabel("Closing reason :");
		$l_noteInput->setCols(60);
		$l_noteInput->setRows(5);
		$this->addElement($l_noteInput);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		$l_cancel=new HTML_QuickForm_submit($this->getButtonName("cancel"));
		$l_cancel->setValue("Cancel");
		$this->addElement($l_cancel);
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Close the action","../../resources/htmledit.php",null);
    	?>
    	<center>
    	<p>Please confirm that you want to change the status to CLOSE.<br/>
    	Please also provide an explanation for the reason why this action is to be closed.
    	</p>
    	<div style="background-color: red;color: white;"><u>WARNING:</u> a closed action cannot be re-opened !</div>
    	<br/>
    	<?php
        $page->display();
        ?>
        </center>
        <?php
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	$l_itemId=base64_decode($page->exportValue("itemId"));
 
    	$l_action=Item::getFromPrimaryKey($l_itemId);
    	
    	//Actions can be modified only by the creator of the action
    	$l_creatingUserId=$l_action->getCreatingUser_id();
		$l_me=SessionManager::getCurrentLoggedUserId();
		if($l_creatingUserId==$l_me){
	    	$l_action->setItemStatus_id(Item::ACTION_STATUS_CLOSED);
	    	$l_action->save();
	    	
	    	$l_comment=new Comment();
	    	$l_comment->setCreated(date("Y/m/d", time()));
	    	$l_comment->setCreatingUser_id(SessionManager::getCurrentLoggedUserId());
	    	$l_comment->setItem_id($l_itemId);
	    	$l_comment->setNote($page->exportValue("note"));
	    	$l_comment->save();
		}
    	
    	HTTP::redirect("index.php");
    }
}

class CancelProcess extends HTML_QuickForm_Action{
    function perform($page){
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('closeActionProcess', false);
$controller->container(true);
$controller->addPage(new CloseActionPage('actionClosingPage'));
if(isset($_REQUEST["id"])){
	$controller->setDefaults(array(
			"itemId" => $_REQUEST["id"]));
}
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('cancel', new CancelProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>